package com.joysuch.wwyt.alert.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.joysuch.wwyt.alert.entity.NoticeWaitingTriggerList;
import com.joysuch.wwyt.alert.enums.GeTuiPartnerTypeEnum;
import com.joysuch.wwyt.alert.enums.TriggerStatusEnum;
import com.joysuch.wwyt.alert.mapper.NoticeWaitingTriggerListMapper;
import com.joysuch.wwyt.alert.service.NoticeWaitingTriggerListService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joysuch.wwyt.common.constant.Constant;
import com.joysuch.wwyt.workflow.bean.notice.TransformedUserGroup;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author heqifeng
 * @since 2021-07-22
 */
@Service
public class NoticeWaitingTriggerListServiceImpl extends ServiceImpl<NoticeWaitingTriggerListMapper, NoticeWaitingTriggerList> implements NoticeWaitingTriggerListService {

    @Autowired
    private NoticeWaitingTriggerListMapper noticeWaitingTriggerListMapper;

    @Override
    public List<NoticeWaitingTriggerList> getNotTriggered() {
        LambdaQueryWrapper<NoticeWaitingTriggerList> searchWrapper = new LambdaQueryWrapper<>();
        searchWrapper.eq(NoticeWaitingTriggerList::getDeleteFlag, "0");
        searchWrapper.le(NoticeWaitingTriggerList::getSendTime, new Date());
        searchWrapper.eq(NoticeWaitingTriggerList::getTriggerStatus, TriggerStatusEnum.NOT_TRIGGERED.getStatus());
        return noticeWaitingTriggerListMapper.selectList(searchWrapper);
    }

    @Override
    public int deleteNotTriggered(String taskCode) {
        NoticeWaitingTriggerList noticeWaiting = new NoticeWaitingTriggerList();
        noticeWaiting.setDeleteFlag("1");
        LambdaQueryWrapper<NoticeWaitingTriggerList> searchWrapper = new LambdaQueryWrapper<>();
        searchWrapper.le(NoticeWaitingTriggerList::getTaskCode, taskCode);
        searchWrapper.eq(NoticeWaitingTriggerList::getTriggerStatus, TriggerStatusEnum.NOT_TRIGGERED.getStatus());
        return noticeWaitingTriggerListMapper.update(noticeWaiting,searchWrapper);
    }

    @Override
    public int save(Set<Long> userIds, String title, String body,Integer businessType, String taskCode,Date sendDate){
        NoticeWaitingTriggerList noticeWaitingTriggerList = new NoticeWaitingTriggerList();
        TransformedUserGroup userGroupObj = new TransformedUserGroup();
        userGroupObj.setKey(GeTuiPartnerTypeEnum.PERSON.getType());
        if (CollectionUtil.isNotEmpty(userIds)) {
            String userGroupValue = userIds.stream().map(String::valueOf).collect(Collectors.joining(Constant.SPLIT_COMMA));
            userGroupObj.setValue(userGroupValue);
            ArrayList<TransformedUserGroup> userGroupList = new ArrayList<>();
            userGroupList.add(userGroupObj);
            String userGroup = JSON.toJSONString(userGroupList);
            noticeWaitingTriggerList.setNoticeUserGroup(userGroup)
                    .setTriggerStatus(TriggerStatusEnum.NOT_TRIGGERED.getStatus())
                    .setNoticeTitle(title)
                    .setNoticeContent(body)
                    .setBusinessType(businessType)
                    .setTaskCode(taskCode)
                    .setSendTime(sendDate == null ? new Date() : sendDate);
            return noticeWaitingTriggerListMapper.insert(noticeWaitingTriggerList);
        }
        return 0;
    }

    @Override
    public boolean saveInRam(Set<Long> userIds, String title, String body,
                             Integer businessType, String taskCode,Date sendDate,
                             List<NoticeWaitingTriggerList> list){
        NoticeWaitingTriggerList noticeWaitingTriggerList = new NoticeWaitingTriggerList();
        TransformedUserGroup userGroupObj = new TransformedUserGroup();
        userGroupObj.setKey(GeTuiPartnerTypeEnum.PERSON.getType());
        if (CollectionUtil.isNotEmpty(userIds)) {
            String userGroupValue = userIds.stream().map(String::valueOf).collect(Collectors.joining(Constant.SPLIT_COMMA));
            userGroupObj.setValue(userGroupValue);
            ArrayList<TransformedUserGroup> userGroupList = new ArrayList<>();
            userGroupList.add(userGroupObj);
            String userGroup = JSON.toJSONString(userGroupList);
            noticeWaitingTriggerList.setNoticeUserGroup(userGroup)
                    .setTriggerStatus(TriggerStatusEnum.NOT_TRIGGERED.getStatus())
                    .setNoticeTitle(title)
                    .setNoticeContent(body)
                    .setBusinessType(businessType)
                    .setTaskCode(taskCode)
                    .setSendTime(sendDate == null ? new Date() : sendDate);
            list.add(noticeWaitingTriggerList);
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteByBusinessTypeAndTaskCode(Integer type, String taskCode) {
        NoticeWaitingTriggerList noticeWaiting = new NoticeWaitingTriggerList();
        noticeWaiting.setDeleteFlag("1");
        LambdaQueryWrapper<NoticeWaitingTriggerList> searchWrapper = new LambdaQueryWrapper<>();
        searchWrapper.eq(NoticeWaitingTriggerList::getTaskCode, taskCode);
        searchWrapper.eq(NoticeWaitingTriggerList::getBusinessType, type);
        return noticeWaitingTriggerListMapper.update(noticeWaiting,searchWrapper) > 0;
    }
}
